var fs = require("fs");
var wavName = "part22";
var wavRead = fs.readFileSync(`./wav/${wavName}.wav`);
console.log(wavRead);
var wavHeader = {};
wavHeader.RIFF = wavRead.slice(0,4).toString();
wavHeader.wavLen = wavRead[4] + wavRead[5]*256 + wavRead[6]*256*256 + wavRead[7]*256*256*256;
wavHeader.WAVE = wavRead.slice(8,12).toString();
wavHeader.fmt_ = wavRead.slice(12,16).toString();
wavHeader.size1 = wavRead[16] + wavRead[17]*256 + wavRead[18]*256*256 + wavRead[19]*256*256*256;
wavHeader.formatTag = wavRead[20] + wavRead[21]*256;
wavHeader.channel = wavRead[22] + wavRead[23]*256;
wavHeader.simpleRate = wavRead[24] + wavRead[25]*256 + wavRead[26]*256*256 + wavRead[27]*256*256*256;
wavHeader.bytePerSec = wavRead[28] + wavRead[29]*256 + wavRead[30]*256*256 + wavRead[31]*256*256*256;
wavHeader.blockAlign = wavRead[32] + wavRead[33]*256;
wavHeader.bitPerSample = wavRead[34] + wavRead[35]*256 + wavRead[36]*256*256 + wavRead[37]*256*256*256;
wavHeader.data = wavRead.slice(38, 42).toString();
wavHeader.size2 = wavRead[42] + wavRead[43]*256 + wavRead[44]*256*256 + wavRead[45]*256*256*256;


console.log(wavHeader);

console.log(typeof wavRead);

var autoScore = 1;

var jiange = 0;
var zsout = 0;
var endNum =  wavRead.length - 44000;
var startIndex = 0;
var isOut = 0;
var voiceCount = 0;
for (var i = 65; i <= endNum; i+=2) {
    // autoScore = ((autoScore + 100) > 65535) ? ((autoScore + 100) - 65535) : ((autoScore + 100))
    // var deepScore = (autoScore - 32768) / 5 | 0
    // wavRead[i] = (autoScore%256 | 0);
    // wavRead[++i] = a = (i % 16) * 16//(autoScore/256 | 0);
    // // console.log(a)
    // wavRead[++i] = (autoScore%256 | 0);
    // wavRead[++i] = b = (i % 16) * 16 + 64//(autoScore/256 | 0);
    
    /**
     * 双声道 语音重叠 alv
     */
    // wavRead[i] = wavRead[i + 162400];
    /**
     * 双声道去噪音 test2.wav
     */
    // if (wavRead[i] > 127) {
    //     wavRead[i] = 255 - (255 - wavRead[i]) * 1;
    //     if (wavRead[i] > 250) {
    //         jiange += 1;
    //         if (jiange > 3000) {
    //             var a = i;
    //             wavRead[i] = 0;
    //             wavRead[i-1] = 0;
    //             // wavRead[i] = 255 - (255 - wavRead[i]) * .2;
    //             jiange -= 1;
    //         }
    //     } else {
    //         zsout += 1;
    //         if (zsout > 50) {
    //             jiange = 0;
    //             zsout = 0;
    //         }
    //     }
    // } else {
    //     wavRead[i] = wavRead[i] * 1;
    //     if (wavRead[i] < 5) {
    //         jiange += 1;
    //         if (jiange > 3000) {
    //             var a = i;
    //             wavRead[i] = 0;
    //             wavRead[i-1] = 0;
    //             // wavRead[i] = wavRead[i] * .2;
    //             jiange -= 1;
    //         }
    //     } else {
    //         zsout += 1;
    //         if (zsout > 50) {
    //             jiange = 0;
    //             zsout = 0;
    //         }
    //     }
    // }
    /**
     * 双声道去噪音 test2.wav 并拿出音节
     */
    if (wavRead[i] > 127) {
        wavRead[i] = 255 - (255 - wavRead[i]) * 1;
        if (wavRead[i] > 250) {
            jiange += 1;
            if (jiange > 3000) {
                var a = i;
                wavRead[i] = 0;
                wavRead[i-1] = 0;
                // wavRead[i] = 255 - (255 - wavRead[i]) * .2;
                jiange -= 1;
            }
        } else {
            zsout += 1;
            if (zsout > 50) {
                jiange = 0;
                zsout = 0;
            }
        }
    } else {
        wavRead[i] = wavRead[i] * 1;
        if (wavRead[i] < 5) {
            jiange += 1;
            if (jiange > 3000) {
                var a = i;
                wavRead[i] = 0;
                wavRead[i-1] = 0;
                // wavRead[i] = wavRead[i] * .2;
                jiange -= 1;
                if (!isOut) {
                    var outCount = ++voiceCount;
                    fs.writeFileSync(`./out/c${outCount}.out.wav`, wavRead.slice(0, 54))
                    fs.appendFileSync(`./out/c${outCount}.out.wav`, wavRead.slice(startIndex+1, i))
                    isOut = 1;
                }
            }
        } else {
            zsout += 1;
            if (zsout > 50) {
                jiange = 0;
                zsout = 0;
                if (isOut) {
                    startIndex = i;
                    isOut = 0;
                }
            }
        }
    }
    // console.log(jiange);
    // console.log(wavRead[i])
}
console.log(wavRead.length);
for (var i = 46; i < 60; i++) {
    wavName[i] = 0;
}
fs.writeFileSync(`./out/${wavName}.out.wav`, wavRead)

// var fStr = "";
// var sumScore = 0;
// var absSumSocre = 0;
// var wavePrint1 = "";
// var wavePrint2 = "";
// var wavePrint3 = "";
// var wavePrint4 = "";
// var wavePrint5 = "";
// const rangeLimit = [300, 450];
// console.log("test.wav lenth " + wavRead.length);
// for (var i = 501; i < wavRead.length -1; i+= 2) {
//     // console.log((127.5*256 - wavRead[i]*256) + wavRead[i+1]);
//     fStr += (127.5*256 - wavRead[i]*256) + wavRead[i+1] + "\r\n";
//     let score = (127.5*256 - wavRead[i]*256) + wavRead[i+1];
//     sumScore += score;
//     absSumSocre += score > 0 ? score : -score;
//     // wavePrint += score > 0 ? "`": "_";
//     wavePrint1 += score >= 15000 ? "·": " ";
//     wavePrint2 += score >= 5000 && score < 15000 ? "·": " ";
//     wavePrint3 += score >= -5000 && score < 5000 ? "·": " ";
//     wavePrint4 += score >= -15000 && score < -5000 ? "·": " ";
//     wavePrint5 += score >= -32768 && score < -15000 ? "·": " ";
//     //fs.appendFileSync("4.csv", (127.5*256 - wavRead[i]*256) + wavRead[i+1] + "\r\n");
// }
// console.log(`test.wav score ${sumScore / wavRead.length}`);
// console.log(`test.wav score[abs] ${absSumSocre / wavRead.length}`);
// console.log(`: ${wavePrint1.substring(rangeLimit[0], rangeLimit[1])}`);
// console.log(`: ${wavePrint2.substring(rangeLimit[0], rangeLimit[1])}`);
// console.log(`: ${wavePrint3.substring(rangeLimit[0], rangeLimit[1])}`);
// console.log(`: ${wavePrint4.substring(rangeLimit[0], rangeLimit[1])}`);
// console.log(`: ${wavePrint5.substring(rangeLimit[0], rangeLimit[1])}`);
// // fs.writeFileSync("4.csv", fStr);
